layui.define(['jquery','form'], function(exports){
    var $ = layui.jquery;
    var form = layui.form;
    var element = layui.element;
    var dictJson = {
        "enableState":[[0,"启用"],[1,"未启用"]],
        "groupType":[[1,"平台自营"],[2,"商户入驻"]],
        "applyState":[[10,"代处理"],[20,"已通过"],[30,"未通过"]],
        "isPackageMail":[[0,"非包邮"],[1,"包邮"]],
        "recommendedState":[[0,"非优选"],[1,"优选"]],
        "goodsState":[[0,"正常"],[1,"从列表下架"],[2,"从仓库下架"]],
        "goodsSource":[[0,"供应商"],[1,"自营"]]
    };
/*    var dictJson = layui.sessionData(layui.setter.tableName).dictJson;*/
    var obj = {
        /**
         *
         * @param treeList
         * 结构(javaBean)：
         * private String name;
         * private String code;
         * //代码类型1-int 2-bool 3-string
         private Integer type;
         private Long parentId;
         private Integer sortOrder;
         private List<BaseCategoryPo> children = new ArrayList<BaseCategoryPo>();
         * @returns 返回格式示例：
         * {
        "status":[[1,"正常"],[2,"禁用"]],
        "yesNot":[[1,"是"],[2,"否"]],
        "bool":[[true,"是"],[false,"否"]]
        };
         */
        treeToDictJson:function (treeList) {
            var rtnJson = {};
            for(var i=0;i<treeList.length;i++){
                var vo= treeList[i];
                var code = vo.code;
                var type = vo.type;
                var children = vo.children;
                if(children){
                    var dictArr = [];
                    for(var j=0;j<children.length;j++){
                        var dictRec = [];
                        var c_vo = children[j];
                        var c_name = c_vo.name;
                        var c_code = c_vo.code;
                        var c_type = c_vo.type;
                        if(type==1){
                            dictRec.push(parseInt(c_code))
                        }else if(type==2){
                            dictRec.push(c_code=='true'?true:false)
                        }else {
                            dictRec.push(c_code);
                        }
                        dictRec.push(c_name);
                        dictArr.push(dictRec)
                    }
                    rtnJson[code] =  dictArr;
                }
            }
            return rtnJson;
        },
        options:function(name){
            var list = dictJson[name];
            if(!list){
                return null;
            }
            return list;
        },
        /**
         * layui 动态渲染select
         * @param id
         * @param name
         * @param defaultVal
         */
        setSelect:function (id,name) {
            var defaultVal = $("#"+id).attr("data");
            $("#"+id).empty();
            $("#"+id).append(new Option("请选择",""));
            var list = obj.options(name);
            if(!list){
                return;
            }
            $.each(list,function(index,item){
                var option = new Option(item[1],item[0]);
                if(item[0]==defaultVal){
                    option.selected=true;
                }
                $("#"+id).append(option)
            });
            layui.form.render("select");
            layui.element.render();
        },
        /**
         * 根据字典分类和值取显示值
         * @param name 字典分类名称code
         * @param val 字典值
         * @returns {string} 显示值
         */
        showName:function (name,val) {
            var list = obj.options(name);
            if(!list){
                return '';
            }
            var html = '';
            $.each(list,function(index,item){
                if(item[0]==val){
                    html = item[1]
                    return;
                }
            });
            return html;
        },

        /**
         * layui 动态渲染radio
         * @param id
         * @param name
         * @param defaultVal
         */
        setRadio:function (id,name,defaultVal) {
            $("#"+id).html("");
            var list = obj.options(name);
            if(!list){
                return;
            }
            $.each(list,function(index,item){
                var radioHtml = '<input type="radio" name="'+name+'" value="'+item[0]+'" title="'+item[1]+'" ';
                if(item[0]==defaultVal){
                    radioHtml+='checked=""';
                }
                radioHtml+='>';
                $("#"+id).append(radioHtml)
            });
            layui.form.render();
            layui.element.render();
        },
        /**
         * layui 动态渲染switch
         * @param name
         * @param val
         */
        setSwitch:function (name,val) {
            if(!val){
                val = false;
            }
            var elem = $("[name="+name+"]");
            if(val==0 || val==false){
                elem.removeAttr("checked");
                elem.val(false)
            }else{
                elem.attr("checked",true);
                elem.val(true)
            }
            form.on('switch(form-switch)', function(data){
                $(data.elem).val(data.elem.checked)
            });

        }
    };
    //输出接口
    exports('dict', obj);
});